package com.example.controller.sys;

import com.example.common.Message;
import com.example.model.ums.UmsAdmin;
import com.example.model.ums.UmsRole;
import com.example.model.vo.UmsRoleVo;
import com.example.service.ums.UmsRoleService;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/sys/umsRole")
@Slf4j
public class UmsRoleController {


    @Autowired
    private UmsRoleService service;

    /**
     * 分页查询
     * @param umsRole
     * @return
     */
    @RequestMapping(value = "/findUmsRolePageByParam", method = RequestMethod.GET)
    @PreAuthorize("hasAuthority('role')")
    @ResponseBody
    public Message findUmsRolePageByParam(UmsRoleVo umsRole) {
        long startTime = System.currentTimeMillis();
        log.info("UmsRoleController.findUmsRolePageByParam 入参 UmsRoleVo ->{},start by startTime ->{}", umsRole, startTime);
        PageInfo<UmsRole> umsAdmins = service.findUmsRolePageByParam(umsRole);
        log.info("UmsRoleController.findUmsRolePageByParam 耗时：{} ms", System.currentTimeMillis() - startTime);
        return Message.ok(umsAdmins);
    }

    /**
     * 条件查询列表
     * @param umsRole
     * @return
     */
    @RequestMapping(value = "/findUmsRolesByParam", method = RequestMethod.GET)
    @PreAuthorize("hasAuthority('role')")
    @ResponseBody
    public Message findUmsRoleByParam(UmsRole umsRole) {
        long startTime = System.currentTimeMillis();
        log.info("UmsRoleController.findUmsRoleByParam 入参 UmsRole ->{},start by startTime ->{}", umsRole, startTime);
        List<UmsRole> umsAdmins = service.findUmsRoleByParam(umsRole);
        log.info("UmsRoleController.findUmsRoleByParam 耗时：{} ms", System.currentTimeMillis() - startTime);
        return Message.ok(umsAdmins);
    }

    /**
     * 新增
     * @param umsRole
     * @return
     */
    @RequestMapping(value = "/insert", method = RequestMethod.POST)
    @PreAuthorize("hasAuthority('role')")
    public Message insert(@RequestBody UmsRole umsRole) throws Exception {
        long startTime = System.currentTimeMillis();
        log.info("UmsRoleController.insert 入参 UmsRole ->{},start by startTime ->{}", umsRole, startTime);
        if (null == umsRole) {
            throw new Exception("参数异常.");
        }
        Integer rlt = service.insert(umsRole);
        log.info("UmsRoleController.insert 耗时：{} ms", System.currentTimeMillis() - startTime);
        return Message.ok(rlt);
    }

    /**
     * 删除
     * @param id
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/deteleById/{id}", method = RequestMethod.GET)
    @PreAuthorize("hasAuthority('role')")
    public Message deteleById(@PathVariable("id") String id) throws Exception {
        long startTime = System.currentTimeMillis();
        log.info("UmsRoleController.deteleById 入参 id ->{},start by startTime ->{}", id, startTime);
        if (null == id) {
            throw new Exception("参数异常.");
        }
        service.deleteById(id);
        log.info("UmsRoleController.deteleById 耗时：{} ms", System.currentTimeMillis() - startTime);
        return Message.ok();
    }

}
